package com.sddz.pacs.controller;

import com.github.pagehelper.PageInfo;
import com.sddz.pacs.annotation.SysLogger;
import com.sddz.pacs.dto.JsonResult;
import com.sddz.pacs.dto.PageModelParams;
import com.sddz.pacs.entity.SysRole;
import com.sddz.pacs.service.SysRolePermissionService;
import com.sddz.pacs.service.SysRoleService;
import com.sddz.pacs.support.ResultConstant;
import com.sddz.pacs.util.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * Author: haoshili
 * Date: Created in 2018/5/20
 * Company: 河北时代电子
 */
@RestController
@RequestMapping("/sysRole")
@Api(description = "系统角色信息表")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @PostMapping("/list")
    @SysLogger(value = "查询用户列表",type = "用户管理")
    @ApiOperation(value="获取角色列表", notes="获取角色分页立标")
    @ApiImplicitParam(name = "pageModelParams",value = "分页参数json对象",required = true)
    public JsonResult list(@RequestBody PageModelParams pageModelParams){
        PageInfo<SysRole> pageInfo = sysRoleService.selectListByCondition(pageModelParams);
        return JsonResult.success(pageInfo,ResultConstant.ROLE_LIST_SUCCESS);
    }

    /**
     * 返回不分页信息
     * @return
     */
    @GetMapping("/listAll")
    @ApiOperation(value="获取角色列表", notes="获取角色分页立标")
    public JsonResult listAll(){
        List<SysRole> sysRoleList = sysRoleService.selectByMap(new HashMap<>());
        return JsonResult.success(sysRoleList,ResultConstant.ROLE_LIST_SUCCESS);
    }

    @PostMapping("/add")
    @SysLogger(value = "新增角色信息",type = "角色管理")
    @ApiOperation(value="新增角色用户")
    @ApiImplicitParam(name = "sysRole",value = "添加成功或失败",required = true)
    public JsonResult add(@RequestBody SysRole sysRole){
        //验证数据的完整性
        ValidatorUtils.validateEntity(sysRole);
        //1.角色信息保存成功，生成Id
        boolean flag = sysRoleService.addRole(sysRole);
        if(!flag){
            return JsonResult.failure(ResultConstant.ADD_FAILURE);
        }
        return JsonResult.success(ResultConstant.ADD_SUCCESS);
    }
    @GetMapping("/delete/{id}")
    @SysLogger(value = "删除角色用户",type = "角色管理")
    @ApiOperation(value="删除角色用户")
    @ApiImplicitParam(name = "id",value = "删除成功或失败",required = true)
    public JsonResult delete(@PathVariable("id") String id){

        boolean flag = sysRoleService.deleteRole(id);
        if(!flag){
            return JsonResult.failure(ResultConstant.DELETE_FAILURE);
        }
        return JsonResult.success(ResultConstant.DELETE_SUCCESS);
    }
    @PostMapping("/update")
    @SysLogger(value = "更新角色用户",type = "角色管理")
    @ApiOperation(value="更新角色用户")
    @ApiImplicitParam(name = "sysRole",value = "修改成功或失败",required = true)
    public JsonResult update(@RequestBody SysRole sysRole){
        //验证数据的完整性
        ValidatorUtils.validateEntity(sysRole);
        boolean flag = sysRoleService.updateRole(sysRole);
        if(!flag){
            return JsonResult.failure(ResultConstant.UPDATE_FAILURE);
        }
        return JsonResult.success(ResultConstant.UPDATE_SUCCESS);
    }

//    @PostMapping("/updatePermission")
//    @RequiresPermissions("sysRole:auth")
//    @SysLogger(value = "授权角色用户",type = "角色管理")
//    @ApiOperation(value="授权角色用户")
//    @ApiImplicitParam(name = "sysRole",value = "授权",required = true)
//    public JsonResult updatePermission(@RequestBody SysRole sysRole){
//        sysRole.setUpdateDate(new Date());
//        sysRoleService.updateById(sysRole);
//        //先删除角色ID所有标识，在保存
//        sysRolePermissionService.deleteById(sysRole.getId());
//
//        boolean flag=sysRolePermissionService.saveRolePermission(sysRole);
//        if(!flag){
//            return JsonResult.failure("授权失败");
//        }
//        return JsonResult.success("授权成功");
//    }
}
